# @Time : 2021/8/4 13:43
# @Author : Li Kunlun
# @Description : 多层感知机
import utils as d2l
from mxnet import autograd, nd
from matplotlib import pyplot as plt


def xyplot(x_vals, y_vals, name):
    """ 绘图函数
    :param
        :param x_vals: x坐标
        :param y_vals: y坐标
        :param name: 函数名
        :return:
    """
    d2l.set_figsize(figsize=(5, 2.5))
    d2l.plt.plot(x_vals.asnumpy(), y_vals.asnumpy())
    d2l.plt.xlabel('x')
    d2l.plt.ylabel(name + '(x)')
    plt.show()


# 1、ReLu函数
x = nd.arange(-8.0, 8.0, 0.1)
x.attach_grad()
with autograd.record():
    y = x.relu()
xyplot(x, y, 'relu')

# 绘制ReLU函数导数图像
y.backward()
xyplot(x, x.grad, 'grad of relu')

# 2、Sigmoid函数
with autograd.record():
    y = x.sigmoid()
xyplot(x, y, 'sigmoid')

# 绘制sigmoid函数导数图像
y.backward()
xyplot(x, x.grad, 'grad of sigmoid')

# 3、tanh函数
with autograd.record():
    y = x.tanh()
xyplot(x, y, 'tanh')

# 绘制tanh函数导数图像
y.backward()
xyplot(x, x.grad, 'grad of tanh')
